<?php
namespace admin\model;

use admin\model\Model;

class TargetModel extends Model{
    /**
     * 获取博客 id 的标签
     */
    public function getTarget($params){
        $sql = 'SELECT blog_target.target FROM blog_target WHERE blog_id=:id';
        return $this->p_queryAll($sql,$params);
    }
     /**
     * 查询标签的数量
     */
    public function getTargetCount($params = null){
        $sql = 'SELECT COUNT(*) AS count FROM blog_target';
        if(isset($params['target'])){
            $sql.=' WHERE target LIKE :target';
            $data = $this->p_query($sql,array('target'=>'%'.$params['target'].'%'));
        }else{
            $data = $this->query($sql);
        }
        return $data;
    }
    /**
     * 查询标签并返回
     */

    public function getTargetAll($params = array('page' => 0,'limit' => 0)){
        $curr = ($params['page']-1)*$params['limit'];
        $sql = "SELECT blog_target.id,blog_info.title,blog_target.target,blog_target.state FROM blog_info,blog_target WHERE blog_info.id=blog_target.blog_id ";
        if(isset($params['target'])){
            $sql.=" AND blog_target.target LIKE :target LIMIT {$curr},{$params['limit']} ";
            $data = $this->p_queryAll($sql,array('target'=>'%'.$params['target'].'%'));
        }else{
            $sql.=" LIMIT {$curr},{$params['limit']} ";
            $data = $this->queryAll($sql);
        }
        return $this->filter($data);
    }

    /**
     * 数据过滤
     */
    public function filter($data){
        $len = count($data,COUNT_NORMAL);       //统计数组长度
        for($i=0;$i<$len;++$i){
            $data[$i]['state'] = $data[$i]['state']==='0'?'不显示':'显示'; 
        }
        return $data;
    }
    /**
     * 更新标签信息
     */
    public function updateTarger($params){
        $data=[];
        $sql = 'UPDATE blog_target SET target = :target , state=:state WHERE id = :id';
        $this->p_execute($sql,$params);
        if($this->db->num == 1 ){
            $data['code'] = '1';
        }else{
            $data['code'] = '0';
        }
        return $data;
    }
    /**
     * 隐藏标签
     */
    public function hideTarget($params){
        $sql = 'UPDATE blog_target SET state=0 WHERE id = :id';
        $this->p_execute($sql,$params);
        if($this->db->num == 1 ){
            $data['code'] = '1';
        }else{
            $data['code'] = '0';
        }
        return $data;
    }
    /**
     * 删除标签
     */
    public function delTarget($params){
        $sql = 'DELETE FROM blog_target WHERE id=:id';
        $this->p_execute($sql,$params);
        if($this->db->num == 1 ){
            $data['code'] = '1';
        }else{
            $data['code'] = '0';
        }
        return $data;
    }

}